//
//  YQShareSDKManager+Share.m
//  MeiMeng
//
//  Created by weixb on 2017/3/24.
//  Copyright © 2017年 weixb. All rights reserved.
//

#import "YQShareSDKManager+Share.h"

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKExtension/SSEShareHelper.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import <ShareSDKUI/SSUIShareActionSheetStyle.h>
//自定义分享编辑界面所需要导入的头文件
#import <ShareSDKUI/SSUIEditorViewStyle.h>
#import <ShareSDKUI/SSUIShareActionSheetCustomItem.h>
#import <ShareSDK/ShareSDK+Base.h>

#import <ShareSDKExtension/ShareSDK+Extension.h>
#import <MOBFoundation/MOBFoundation.h>
//#import <AssetsLibrary/AssetsLibrary.h>

@implementation YQShareSDKManager (Share)

/**
 *  分享
 *
 *  @param text   @"分享内容"
 *  @param images 图片集合,传入参数可以为单张图片信息，也可以为一个NSArray，数组元素可以为UIImage、NSString（图片路径）、NSURL（图片路径）、SSDKImage。
 *  @param urlStr 网页地址
 *  @param title  @"分享标题"
 */
+ (void)shareByText:(NSString *)text images:(id)images urlStr:(NSString *)urlStr title:(NSString *)title {
    //1、创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:text
                                     images:images
                                        url:[NSURL URLWithString:urlStr]
                                      title:title
                                       type:SSDKContentTypeAuto];
    
    
    // 定制微信好友的分享内容
    //    [shareParams SSDKSetupWeChatParamsByText:@"定制微信的分享内容"
    //                                       title:@"title"
    //                                         url:[NSURL URLWithString:@"http://mob.com"]
    //                                  thumbImage:nil
    //                                       image:[UIImage imageNamed:@"传入的图片名"]
    //                                musicFileURL:nil
    //                                     extInfo:nil
    //                                    fileData:nil
    //                                emoticonData:nil
    //                                        type:SSDKContentTypeAuto  forPlatformSubType:SSDKPlatformSubTypeWechatSession];// 微信好友子平台
    //定义分享平台
    NSArray *items = @[
                       @(SSDKPlatformTypeSMS),
                       @(SSDKPlatformTypeSinaWeibo),
                       @(SSDKPlatformSubTypeWechatSession),
                       @(SSDKPlatformSubTypeWechatTimeline),
                       @(SSDKPlatformTypeQQ)
                       ];
    // 设置分享菜单栏样式（非必要）
    // 设置分享菜单的背景颜色
//    [SSUIShareActionSheetStyle setActionSheetBackgroundColor:[UIColor colorWithRed:249/255.0 green:0/255.0 blue:12/255.0 alpha:0.5]];
//    // 设置分享菜单颜色
//    [SSUIShareActionSheetStyle setActionSheetColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0]];
//    // 设置分享菜单－取消按钮背景颜色
//    [SSUIShareActionSheetStyle setCancelButtonBackgroundColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0]];
//    // 设置分享菜单－取消按钮的文本颜色
//    [SSUIShareActionSheetStyle setCancelButtonLabelColor:[UIColor blackColor]];
//    // 设置分享菜单－社交平台文本颜色
//    [SSUIShareActionSheetStyle setItemNameColor:[UIColor whiteColor]];
//    // 设置分享菜单－社交平台文本字体
//    [SSUIShareActionSheetStyle setItemNameFont:[UIFont systemFontOfSize:10]];
//    //设置分享编辑界面的导航栏颜色
//    [SSUIEditorViewStyle setiPhoneNavigationBarBackgroundColor:[UIColor blackColor]];
//    //设置编辑界面标题颜色
//    [SSUIEditorViewStyle setTitleColor:[UIColor redColor]];
//    //设置取消发布标签文本颜色
//    [SSUIEditorViewStyle setCancelButtonLabelColor:[UIColor blueColor]];
//    [SSUIEditorViewStyle setShareButtonLabelColor:[UIColor blueColor]];
//    //设置分享编辑界面状态栏风格
//    [SSUIEditorViewStyle setStatusBarStyle:UIStatusBarStyleLightContent];
    //设置简单分享菜单样式
    [SSUIShareActionSheetStyle setShareActionSheetStyle:ShareActionSheetStyleSimple];

    //2、分享（可以弹出我们的分享菜单和编辑界面）
    SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:nil
                                                                     items:items
                                                               shareParams:shareParams
                                                       onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end)
                                             {
                                                 switch (state) {
                                                         
                                                     case SSDKResponseStateBegin:
                                                     {
                                                         //                           [theController showLoadingView:YES];
                                                         break;
                                                     }
                                                     case SSDKResponseStateSuccess:
                                                     {
                                                         //Facebook Messenger、WhatsApp等平台捕获不到分享成功或失败的状态，最合适的方式就是对这些平台区别对待
                                                         if (platformType == SSDKPlatformTypeFacebookMessenger)
                                                         {
                                                             break;
                                                         }
#pragma clang diagnostic push
#pragma clang diagnostic ignored"-Wdeprecated-declarations"
                                                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                                                             message:nil
                                                                                                            delegate:nil
                                                                                                   cancelButtonTitle:@"确定"
                                                                                                   otherButtonTitles:nil];
                                                         [alertView show];
                                                         break;
                                                     }
                                                     case SSDKResponseStateFail:
                                                     {
                                                         if (platformType == SSDKPlatformTypeSMS && [error code] == 201)
                                                         {
                                                             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                                                             message:@"失败原因可能是：1、短信应用没有设置帐号；2、设备不支持短信应用；3、短信应用在iOS 7以上才能发送带附件的短信。"
                                                                                                            delegate:nil
                                                                                                   cancelButtonTitle:@"OK"
                                                                                                   otherButtonTitles:nil, nil];
                                                             [alert show];
                                                             break;
                                                         }
                                                         else if(platformType == SSDKPlatformTypeMail && [error code] == 201)
                                                         {
                                                             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                                                             message:@"失败原因可能是：1、邮件应用没有设置帐号；2、设备不支持邮件应用；"
                                                                                                            delegate:nil
                                                                                                   cancelButtonTitle:@"OK"
                                                                                                   otherButtonTitles:nil, nil];
                                                             [alert show];
                                                             break;
                                                         }
                                                         else
                                                         {
                                                             UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                                                             message:[NSString stringWithFormat:@"%@",error]
                                                                                                            delegate:nil
                                                                                                   cancelButtonTitle:@"OK"
                                                                                                   otherButtonTitles:nil, nil];
                                                             [alert show];
                                                             break;
                                                         }
                                                         break;
                                                     }
                                                     case SSDKResponseStateCancel:
                                                     {
                                                         UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消"
                                                                                                             message:nil
                                                                                                            delegate:nil
                                                                                                   cancelButtonTitle:@"确定"
                                                                                                   otherButtonTitles:nil];
                                                         [alertView show];
                                                         break;
                                                     }
                                                     default:
                                                         break;
                                                 }
#pragma clang diagnostic pop
                                                 if (state != SSDKResponseStateBegin)
                                                 {
                                                     //                       [theController showLoadingView:NO];
                                                     //                       [theController.tableView reloadData];
                                                 }
                                             }];
    //删除和添加平台示例
    //    [sheet.directSharePlatforms removeObject:@(SSDKPlatformTypeQQ)];//(默认微信，QQ，QQ空间都是直接跳客户端分享，加了这个方法之后，可以跳分享编辑界面分享)
    [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeMail)];//（加了这个方法之后可以不跳分享编辑界面，直接点击分享菜单里的选项，直接分享）
    [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSMS)];
    
}

@end
